package cn.itsource.pet.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.baidu.DistanceUtil;
import cn.itsource.pet.domain.OpaDto;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.shop.domain.Shop;
import cn.itsource.shop.mapper.ShopMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 万能键 alt + enter
 */
@Service //交给spring管理
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg, SearchMasterMsgQuery> implements ISearchMasterMsgService {

    /**
     * 注入mapper层对象
     */
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    /**
     * 注入mapper层对象
     */
    @Autowired
    private ShopMapper shopMapper;
    /**
     * 注入mapper层对象
     */
    @Autowired
    private OrderPetAcquisitionMapper opaMapper;


    @Override
    public PageList<SearchMasterMsg> findPage(SearchMasterMsgQuery q) {

        /**
         * 思路
         * 1.查询所有的数据
         * 2.所有的数据一条条按照5km筛选
         * 3.筛选完取前5条
         *
         * 细节在哪?
         */

        //3.获取到店铺的地址
        Long shopId = GetUserInfoUtil.getShopId();
        q.setShopId(shopId);


        //1.调用查询所有
        List<SearchMasterMsg> allMsgs = searchMasterMsgMapper.findPage(q);
        if (CollectionUtil.isEmpty(allMsgs)){
            return new PageList<>();
        }

        //2.所有的数据进行筛选
        //ArrayList<SearchMasterMsg> list = new ArrayList<>();


        //4.通过shopId查店铺,就是t_shop表的主键id
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        String shopAddress = shop.getAddress();

//        for (SearchMasterMsg searchMasterMsg : allMsgs) {
//            //根据5km条件来筛选,如果满足5km内,有效数据
//            boolean inCircle = DistanceUtil.isInCircle(shopAddress, searchMasterMsg.getAddress(), 5000);
//            if (inCircle){
//                list.add(searchMasterMsg);
//            }
//        }
        //filter:过滤 条件:5000之内 collect:收集   Collectors.toList():收集的类型
        List<SearchMasterMsg> msgList = allMsgs.stream().filter(msg -> DistanceUtil.isInCircle(shopAddress, msg.getAddress(), 5000))
                .collect(Collectors.toList());

        if (CollectionUtil.isEmpty(msgList)){
            return new PageList<>();
        }

        //5.第一页的 :怎么取前5条,告诉前端一个:条数
        //  第一页的 :怎么取6-10条,告诉前端一个:总条数
        // (page - 1) *size   size

        //第第几个索引开始取数据
//        Integer startIndex = (q.getPage() - 1) * q.getSize();
//        List<SearchMasterMsg> msgs = msgList.subList(startIndex, startIndex + q.getSize());
//        Integer total = msgList.size();

        // limit (page - 1) *size ,size

//        System.out.println(msgs);
//        select * from t_search_master_msg  limit 0,5
//        select * from t_search_master_msg  limit 5,5
        //skip:跳过的意思                            limit             0                                       5  select * from t_search_master_msg  limit 0,5
        //                                                           5                                       5  select * from t_search_master_msg  limit 5,5
        Stream<SearchMasterMsg> msgResult = msgList.stream().skip((q.getPage() - 1) * q.getSize()).limit(q.getSize());
        List<SearchMasterMsg> list = msgResult.collect(Collectors.toList());

        return new PageList<SearchMasterMsg>((long)msgList.size(),list);
    }

    @Override
    @Transactional
    public AjaxResult jieDan(OpaDto opaDto) {
        //1.根据接单表的id主键,去查询接单表对象
        Long id = opaDto.getId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectByPrimaryKey(id);

        //1.1需要修改接单表的装态,为已接单
        searchMasterMsg.setState(1);
        searchMasterMsgMapper.updateByPrimaryKeySelective(searchMasterMsg);


        //2.把字段copy到接单对象,手动设置对象
        //3.补全接单其他的资源
        OrderPetAcquisition acquisition = new OrderPetAcquisition();
        acquisition.setId(null);
        acquisition.setDigest(opaDto.getNote());
        acquisition.setState(0);
        acquisition.setPrice(BigDecimal.valueOf(searchMasterMsg.getPrice()));
        //订单号
        String orderSn = new Snowflake().nextIdStr();
        acquisition.setOrderSn(orderSn);
        acquisition.setUserId(searchMasterMsg.getUserId());
        acquisition.setPayType(0);
        acquisition.setShopId(searchMasterMsg.getShopId());
        acquisition.setAddress(searchMasterMsg.getAddress());
        acquisition.setEmpId(opaDto.getEmpId());

        //4.新增接单数据,入库搞定
        opaMapper.insertSelective(acquisition);

        return AjaxResult.createSuccess();
    }

    public static void main(String[] args) {
        long l = new Snowflake().nextId();
        long l1 = new Snowflake().nextId();
        long l2 = new Snowflake().nextId();

        System.out.println(l);
        System.out.println(l1);
        System.out.println(l2);
        String s = new Snowflake().nextIdStr();
        String s1 = new Snowflake().nextIdStr();
        String s2 = new Snowflake().nextIdStr();

        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
    }
}
